package me.send.produce.data.controller;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;

import javax.servlet.http.HttpServletRequest;

import me.send.business.base.Pager;
import me.send.data.service.pojo.ComplainBroker;
import me.send.data.service.pojo.base.DatatablesViewPage;
import me.send.data.service.service.ComplainBrokerService;
import me.send.data.service.service.TreeService;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

@Controller
@RequestMapping(value = "complain")
public class ComplainBrokerController {

	@Autowired
	private ComplainBrokerService complainBrokerService;

	@Autowired
	private TreeService treeService;

	@RequestMapping(value = "user/list")
	public ModelAndView complain_user_list(HttpServletRequest request) {
		ModelAndView modelAndView = new ModelAndView(
				"stage/user/complain_broker");
		modelAndView.addObject("LoadTree",
				treeService.main_tree(0, request, "complain/user/list"));
		modelAndView.addObject("module", "用户投诉列表");
		return modelAndView;
	}
	
	
	
	
	@RequestMapping(value="user/page", method=RequestMethod.GET)  
	@ResponseBody  
	public DatatablesViewPage<ComplainBroker> user_page(HttpServletRequest request){  
//获取分页控件的信息  
		String start = request.getParameter("start");  
		String length = request.getParameter("length"); 
//获取前台额外传递过来的查询条件  
		SimpleDateFormat sdf=new SimpleDateFormat("yyyy/MM/dd HH:mm");
		SimpleDateFormat format=new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
		String start_time = request.getParameter("start_time"); 
		String end_time = request.getParameter("end_time");  
		String broker_phone = request.getParameter("broker_phone");  
		String user_phone = request.getParameter("user_phone");  
		if(broker_phone==null||(broker_phone!=null&&"".equals(broker_phone.trim()))){
			broker_phone=null;
		}
		if(user_phone==null||(user_phone!=null&&"".equals(user_phone.trim()))){
			user_phone=null;
		}
		if(start_time!=null&&!"".equals(start_time.trim())&&end_time!=null&&!"".equals(end_time.trim())){
			Date startDate=null;
			Date endDate=null;
			try {
				startDate=sdf.parse(start_time);
				start_time=format.format(startDate);
				endDate=sdf.parse(end_time);
				end_time=format.format(endDate);
			} catch (ParseException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
		}else{
			start_time=null;
			end_time=null;
		}
		
		Pager page=new Pager();
		Integer row=Integer.parseInt(length);
		page.setRows(row);
		Integer startInt=Integer.parseInt(start);
		Integer p=(startInt/row)+1;
		page.setPage(p);
		
		//随便组织的查询结果  
		List<ComplainBroker> list=complainBrokerService.list(user_phone,broker_phone,start_time,end_time,page); 
		Integer count=complainBrokerService.count(user_phone,broker_phone,start_time,end_time); 
		DatatablesViewPage<ComplainBroker> view = new DatatablesViewPage<ComplainBroker>();  
		view.setiTotalDisplayRecords(count);  
		view.setiTotalRecords(count);  
		
		view.setAaData(list);  
		return view;  
	}
	
}
